//
//  LGInProgressView.swift
//  LGMusic
//
//  Created by 梁尚嘉 on 2017/4/6.
//  Copyright © 2017年 LimeGah. All rights reserved.
//

import UIKit
import CoreMedia

func countTime(_ time:Double) -> String {
    let minute = Int(time/60)
    let second = Int((time/60-Double(minute))*60)
    let timeStr = String.init(format: "%02d:%02d", minute,second)
    return timeStr
}

class LGInProgressView: UIView {

    var currentDuration:Double? {
        didSet {
            if !((self.playProgressSlider?.isTouchInside)!) {
                self.playProgressSlider?.setValue(Float(currentDuration!), animated: true)
            }
            self.leftTimeLb?.text = countTime(currentDuration!)
            self.rightTimeLb?.text = countTime(totoalDuration!-currentDuration!)
        }
    }
    var totoalDuration:Double? {
        didSet {
            if (totoalDuration != nil && !(totoalDuration?.isNaN)!) {
                self.playProgressSlider?.maximumValue = Float(totoalDuration!)

            }
        }
    }





    @objc private func didUserChangeProgress(_ sender:UISlider) {
        MusicPlayerManager.sharedInstance.player?.seek(to: CMTimeMake(Int64(sender.value),1))
    }

    private var leftTimeLb:UILabel?
    private var rightTimeLb:UILabel?
    private var playProgressSlider:UISlider?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.configure()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure() {
        self.leftTimeLb = UILabel.init()
        self.leftTimeLb?.font = SM_CFont(11)
        self.leftTimeLb?.textColor = SM_HEXCOLOR("000000")
        self.leftTimeLb?.text = "00:00"
        self.leftTimeLb?.textAlignment = NSTextAlignment.center
        self.addSubview(self.leftTimeLb!)
        self.leftTimeLb?.snp.makeConstraints({ (make) in
            make.left.equalTo(SM_SIZE(40))
            make.centerY.equalTo(self.snp.centerY)
            make.width.equalTo(SM_SIZE(80))
        })

        self.rightTimeLb = UILabel.init()
        self.rightTimeLb?.font = SM_CFont(11)
        self.rightTimeLb?.textColor = SM_HEXCOLOR("000000")
        self.rightTimeLb?.text = "04:31"
        self.rightTimeLb?.textAlignment = NSTextAlignment.center
        self.addSubview(self.rightTimeLb!)
        self.rightTimeLb?.snp.makeConstraints({ (make) in
            make.right.equalTo(SM_SIZE(-40))
            make.centerY.equalTo(self.snp.centerY)
            make.width.equalTo(SM_SIZE(100))
        })


        self.playProgressSlider = UISlider.init(frame: CGRect.zero)
        self.playProgressSlider?.minimumTrackTintColor = SM_HEXCOLOR("4A4A4A")
        self.playProgressSlider?.maximumTrackTintColor = SM_HEXCOLOR("D8D8D8")
        self.playProgressSlider?.setThumbImage(UIImage.init(named: "ic_progress_slide_thumb"), for: UIControlState.normal)
        self.playProgressSlider?.setThumbImage(UIImage.init(named: "ic_progress_slide_thumb"), for: UIControlState.highlighted)
        self.playProgressSlider?.maximumValue = 100
        self.playProgressSlider?.minimumValue = 0
        self.playProgressSlider?.value = 0
        self.playProgressSlider?.addTarget(self, action: #selector(LGInProgressView.didUserChangeProgress(_:)), for: UIControlEvents.valueChanged)
        self.addSubview(self.playProgressSlider!)
        self.playProgressSlider?.snp.makeConstraints({ (make) in
            make.left.equalTo((self.leftTimeLb?.snp.right)!).offset(SM_SIZE(14))
            make.centerY.equalTo(self.snp.centerY)
            make.height.equalTo(SM_SIZE(15))
            make.right.equalTo((self.rightTimeLb?.snp.left)!).offset(SM_SIZE(-14))
        })
    }

}
